package com.jiangruan.javaPro.service;

import com.alibaba.fastjson.JSONObject;
import com.jiangruan.javaPro.Constants;
import com.jiangruan.javaPro.net.HttpUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:冷风诉离歌
 * @desc:该类用于实现调用比特币客户端的getbesthlockhash的RPC功能
 * @datetime: 2021/12/20 16:08
 */

public class GetBestBlockHashService {

    public String getBestBlockHash(){

        //准备用户身份信息
        String author = Constants.BITCOIN_PRC_USER+":"+Constants.BITCOIN_PRC_PASSWORD;
        Base64.Encoder encoder = Base64.getEncoder();
        String authorBase64 = encoder.encodeToString(author.getBytes());
        //准备请求头信息
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic " + authorBase64);


        //RPC请求格式
        Map<String,Object> data =new HashMap<>();
        data.put("id",1001);
        data.put("method","getbestblockhash");
        data.put("params",null);
        data.put("json_rpc","2.0");
        byte[] dataBytes = JSONObject.toJSONBytes(data);


        String result = HttpUtils.post(Constants.BITCOIN_RPC_URL,header,dataBytes);
        
        return null;
    }
}
